package com.musiklegal;



import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class TopUp extends Activity
{
	ProgressDialog dialog;
	String temp= "";
	String test= "";
	CheckBox c;
	String ex = "FALSE";
	TextView des;
	 String [] name ;
	 String [] id ;
	 String [] desc;
	 String [] code;
	 String [] label;
	 String [] key;
	 String [] paket;
	 SharedPreferences tes;
	 String choice;
	 int fachri;
	 AlertDialog.Builder builder; 
	 
	 public void onResume()
		{
		 	super.onResume();
			if(!MusikLegal.internet(getApplicationContext()))
			{
				
				finish();
			}
			
		}
	 
	public void onCreate(Bundle savedInstanceState) 
    {
		if(!MusikLegal.internet(getApplicationContext()))
			finish();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.topup);
        c = (CheckBox) findViewById(R.id.ext);
        builder = new AlertDialog.Builder(this);
		 Button top = (Button) this.findViewById(R.id.top);
		 Button pak = (Button) this.findViewById(R.id.paket);  
		 des = (TextView) findViewById(R.id.des);
		 tes = getSharedPreferences("CurrentUser", MODE_PRIVATE);
		 if(MusikLegal.internet(getApplicationContext())){
		 try{
	        	
				HttpClient mlist = new DefaultHttpClient(); 
				HttpGet get = new HttpGet("http://secure.musiklegal.com/voucher/topup_channel");
				HttpResponse mresp = mlist.execute(get);

				HttpEntity merclist = mresp.getEntity();
				SAXParserFactory spf = SAXParserFactory.newInstance();
				SAXParser sp = spf.newSAXParser();
				XMLReader xr = sp.getXMLReader();
				 
				TopPars xmlr = new TopPars();
				xr.setContentHandler(xmlr);
				xr.parse(retrieveInputStream(merclist)); 
				
				Top tp = xmlr.getTop();
				
			
				name=tp.getName().split("fachr1");
				id=tp.getId().split("fachr1");
				desc=tp.getDes().split("fachr1");
				label=tp.getLabel().split("fachr1");
				code=tp.getCode().split("fachr1");
				key=tp.getKey().split("fachr1");
				des.setText(desc[0]);
				paket = new String[name.length];
				 for(int i = 0; i<name.length;i++)
				 {
					 temp=name[i]+" "+label[i];
					 paket[i]=temp;
				 }
				 
		 	}
			 catch (Exception e) {
				 
		        }
		 }
		 else
	        {
	        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
			alertDialog.setTitle("Error");
			alertDialog.setMessage(Settings.gakonek);
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
				public void onClick(DialogInterface dialog, int which) {
				finish();
				}
			});
			alertDialog.setIcon(R.drawable.oc2);
			alertDialog.show();
	        }
		 
		 
		 top.setOnClickListener(new OnClickListener() {
				
				public void onClick(View arg0) {
					//tryTop();
					if(c.isChecked())
						ex="TRUE";
					else 
						ex="FALSE";
					String p = key[fachri].replace("%UID%", ("AD-"+((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId()));
					p=p.replace("%EXTENSION%",ex); 
					p=p.replace("%USERNAME%",tes.getString("UserName", "default"));
					//Toast.makeText(getApplicationContext(), code[fachri], Toast.LENGTH_SHORT).show();
					sendSMS(code[fachri],p);
				}

			});
		 pak.setOnClickListener(new OnClickListener() {
				
				public void onClick(View arg0) {
					  
			        	builder.setTitle( "Paket" );
			        	builder.setItems(paket , new DialogInterface.OnClickListener() {
			                public void onClick(DialogInterface dialog, int item) {
			                   fachri=item;
			                   choice = paket[item];
			                   final TextView pakt = (TextView) findViewById(R.id.pak);
			  				   pakt.setText(choice);
			                }});
			        	builder.create();
			        	builder.show();
				}
				
			});
	    }
	
	 private InputSource retrieveInputStream(HttpEntity httpEntity) {
	        InputSource insrc = null;
	        try {
	              insrc = new InputSource(httpEntity.getContent());
	        } catch (Exception e) {
	        }
	        return insrc;
	  } 
	    private void sendSMS(String phoneNumber, String message)
	    {        
	       
	    	
	    	
	        PendingIntent pi = PendingIntent.getActivity(this, 0,
	            new Intent(this, Object.class), 0);                
	        SmsManager sms = SmsManager.getDefault();
	        sms.sendTextMessage(phoneNumber, null, message, pi, null); 
	        finish();
	        Toast.makeText(getApplicationContext(),"Terima kasih, Permintaan Berlangganan kamu akan segera di proses", Toast.LENGTH_SHORT).show();
	    	
	    }  
}